﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Paint.DefinePaintBrush.CUiDefineBrushesWindow
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.CodeDom.Compiler;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui.WPF;


namespace DnProcessSimulateCommands.Paint.DefinePaintBrush;

public class CUiDefineBrushesWindow : TxWindow, IComponentConnector
{
  private CApBrushViewModel _viewModel;
  private CUiBrushWindow _brushWindow;
  internal Button _createButton;
  internal Button _deleteButton;
  internal Button _editButton;
  internal DataGrid _brushesGrid;
  internal Button _cancelBtn;
  private bool _contentLoaded;

  public CUiDefineBrushesWindow()
  {
    this.InitializeComponent();
    if (this.IsInDesignMode)
      return;
    this._viewModel = new CApBrushViewModel();
    ((FrameworkElement) this).DataContext = (object) this._viewModel;
  }

  private void TxWindow_Loaded(object sender, RoutedEventArgs e)
  {
    if (!this.IsInDesignMode)
      this.RegisterForEvents();
    this.EnableToolbarButtons();
    this.SetGridRowColorText();
  }

  private void TxWindow_Closed(object sender, EventArgs e)
  {
    this._viewModel.ClearGhostBrush();
    this.UnregisterForEvents();
  }

  private void CreateButton_Click(object sender, RoutedEventArgs e)
  {
    this._viewModel.ClearGhostBrush();
    this._viewModel.CurrentBrush = (CApPaintBrush) null;
    ((Window) new CUiBrushWindow(this._viewModel, (TxWindow) this)).Show();
  }

  private void Close_Button_Click(object sender, RoutedEventArgs e) => ((Window) this).Close();

  private void EditButton_Click(object sender, RoutedEventArgs e)
  {
    this._viewModel.ClearGhostBrush();
    ((Window) new CUiBrushWindow(this._viewModel, (TxWindow) this)).Show();
  }

  private void DeleteButton_Click(object sender, RoutedEventArgs e)
  {
    if (this._brushesGrid.SelectedIndex <= -1)
      return;
    this._viewModel.DeleteBrush();
  }

  private void BrushesGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
    this._viewModel.CurrentBrush = this._brushesGrid.SelectedIndex != -1 ? (CApPaintBrush) this._brushesGrid.SelectedItem : (CApPaintBrush) null;
    this.EnableToolbarButtons();
    this._viewModel.ShowGhostBrush();
  }

  private void _viewModel_BrushAdded(object sender)
  {
    this._brushesGrid.SelectedItem = (object) (CApPaintBrush) sender;
  }

  private void _viewModel_BrushDeleted(object sender)
  {
    this._viewModel.CurrentBrush = (CApPaintBrush) this._brushesGrid.SelectedItem;
    this.EnableToolbarButtons();
    this._viewModel.ShowGhostBrush();
  }

  private void _viewModel_BrushEdited(object sender)
  {
    this.SetGridRowColorText();
    this._viewModel.ShowGhostBrush();
  }

  private void EnableToolbarButtons()
  {
    if (this._viewModel.CurrentBrush == null)
    {
      this._deleteButton.IsEnabled = false;
      this._editButton.IsEnabled = false;
    }
    else
    {
      this._deleteButton.IsEnabled = true;
      this._editButton.IsEnabled = true;
    }
  }

  private void SetGridRowColorText()
  {
    foreach (CApPaintBrush capPaintBrush in (IEnumerable) this._brushesGrid.Items)
    {
      if (capPaintBrush.Solid != null && !TxFrame.op_Equality(capPaintBrush.OriginFrame, (ITxObject) null))
        TxPaintThicknessFunctionHyperLoader.The.LoadedFunctions.Contains(capPaintBrush.Name);
    }
  }

  private void RegisterForEvents()
  {
    ((Window) this).Closing += new CancelEventHandler(this._viewModel.OnWindowClosing);
    this._viewModel.BrushDeleted += new CApBrushViewModel.DeletedEventHandler(this._viewModel_BrushDeleted);
    this._viewModel.BrushAdded += new CApBrushViewModel.DeletedEventHandler(this._viewModel_BrushAdded);
    this._viewModel.BrushEdited += new CApBrushViewModel.DeletedEventHandler(this._viewModel_BrushEdited);
  }

  private void UnregisterForEvents()
  {
    ((Window) this).Closing -= new CancelEventHandler(this._viewModel.OnWindowClosing);
    this._viewModel.BrushDeleted -= new CApBrushViewModel.DeletedEventHandler(this._viewModel_BrushDeleted);
    this._viewModel.BrushAdded -= new CApBrushViewModel.DeletedEventHandler(this._viewModel_BrushAdded);
    this._viewModel.BrushEdited -= new CApBrushViewModel.DeletedEventHandler(this._viewModel_BrushEdited);
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  public void InitializeComponent()
  {
    if (this._contentLoaded)
      return;
    this._contentLoaded = true;
    Application.LoadComponent((object) this, new Uri("/DnProcessSimulateCommands;component/paint/definepaintbrush/cuidefinebrusheswindow.xaml", UriKind.Relative));
  }

  [DebuggerNonUserCode]
  [GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
  [EditorBrowsable(EditorBrowsableState.Never)]
  void IComponentConnector.Connect(int connectionId, object target)
  {
    switch (connectionId)
    {
      case 1:
        ((FrameworkElement) target).Loaded += new RoutedEventHandler(this.TxWindow_Loaded);
        ((Window) target).Closed += new EventHandler(this.TxWindow_Closed);
        break;
      case 2:
        this._createButton = (Button) target;
        this._createButton.Click += new RoutedEventHandler(this.CreateButton_Click);
        break;
      case 3:
        this._deleteButton = (Button) target;
        this._deleteButton.Click += new RoutedEventHandler(this.DeleteButton_Click);
        break;
      case 4:
        this._editButton = (Button) target;
        this._editButton.Click += new RoutedEventHandler(this.EditButton_Click);
        break;
      case 5:
        this._brushesGrid = (DataGrid) target;
        this._brushesGrid.SelectionChanged += new SelectionChangedEventHandler(this.BrushesGrid_SelectionChanged);
        break;
      case 6:
        this._cancelBtn = (Button) target;
        this._cancelBtn.Click += new RoutedEventHandler(this.Close_Button_Click);
        break;
      default:
        this._contentLoaded = true;
        break;
    }
  }
}
